<?php
/**
 * ClassName: ArchivesService
 * Description: 患者档案Service服务类
 * @Author 周昌
 * @Create 2023/11/27 11:43
 */

namespace service\admin;


use libs\Tool;
use model\ArchivesModel;
use model\MemberModel;
use service\BaseService;
use think\Db;

class ArchivesService extends BaseService
{
    protected static $model = 'model\ArchivesModel';
    public static $relation = ['本人','配偶','父母','子女','兄弟','姐妹','其他'];
    public static $education = [1=>'小学',2=>'初中',3=>'中专/高中',4=>'专科',5=>'本科',6=>'硕士研究生',7=>'博士研究生'];

    //人物关系下拉选择
    public static function archivesRelationSelect($key = 0)
    {
        static $archives_relation_select = [];
        if (empty($archives_relation_select)) {
            foreach (self::$relation as $k => $v) {
                if ($k >= $key) {
                    $row = [];
                    $row['id'] = $k;
                    $row['name'] = $v;
                    $archives_relation_select[] = $row;
                }
            }
            array_unshift($archives_relation_select, ['id' => '', 'name' => '-----请选择-----']);
        }
        return $archives_relation_select;
    }
    //学历下拉选择
    public static function archivesEducationSelect($key = 0)
    {
        static $archives_education_select = [];
        if (empty($archives_education_select)) {
            foreach (self::$education as $k => $v) {
                if ($k >= $key) {
                    $row = [];
                    $row['id'] = $k;
                    $row['name'] = $v;
                    $archives_education_select[] = $row;
                }
            }
            array_unshift($archives_education_select, ['id' => '', 'name' => '-----请选择-----']);
        }
        return $archives_education_select;
    }
    //获取家庭组
    public static function getFamilyGroup($where)
    {
        $member_info=MemberModel::getInfo($where,'id,real_name,phone');
        if($member_info){
            $member_info['family_group']=$member_info['real_name'].'-'.$member_info['phone'];
            return Tool::Success('成功',$member_info);
        }else{
            return Tool::Error();
        }
    }
    //获取患者档案信息
    public static function getArchivesInfo($id)
    {
        $info=self::$model::getInfo(['id'=>$id]);
        $member_info=self::getFamilyGroup(['id'=>$info['member_id']])['data'];
        $info['family_group']=$member_info['family_group'];
        $info['phone']=$member_info['phone'];
        return $info;
    }

    //列表数据
    public static function getAjaxData($where)
    {
        $table = self::$model::order('id desc');
        return self::tableJsonData($table, $where, function ($data) {
            $data['height'] = $data['height'] ?: '';
            $data['weight'] = $data['weight'] ?: '';
            $data['sex_name'] = MemberService::$sex[$data['sex']];
            $data['relation_name'] = self::$relation[$data['relation']];
            $data['hospital_name'] = $data['hospital_id'] ? HospitalService::getNameByKey($data['hospital_id'], '') : '';
            return $data;
        },function ($data){
            if(!empty($data)){
                // 批量获取家庭组
                $member_ids=array_column($data,'member_id');
                $family=MemberService::getMoreFamilyGroup([['id','in',$member_ids]]);
                foreach ($data as &$v){
                    $v['family_group']=isset($family[$v['member_id']])?$family[$v['member_id']]['family_group']:'';
                }
            }
            return $data;
        });
    }

}